import 'package:json_annotation/json_annotation.dart';

part 'base_model.g.dart';
///基础模型
@JsonSerializable(genericArgumentFactories: true)
class BaseModel<T> {
  final int? code;
  final String? msg;
  final T? data;

  @JsonKey(fromJson: _parseMap) // 添加自定义转换
  final Map<dynamic, dynamic>? extra;

  BaseModel(this.code, this.msg, this.data, this.extra);

  // 自定义转换方法 适配后端 处理{}的问题
  static Map<dynamic, dynamic>? _parseMap(Object? json) {
    if (json == null) return null;
    if (json is Map<dynamic, dynamic>) return json;
    if (json is Map) return Map<dynamic, dynamic>.from(json);
    return null;
  }

  // 从JSON 创建实例
  factory BaseModel.fromJson(Map<String, dynamic> json, T Function(Object?) fromJsonT) =>
      _$BaseModelFromJson(json, fromJsonT);

  Map<String, dynamic> toJson(Object Function(T value) toJsonT) => _$BaseModelToJson(this, toJsonT);

// DateTime get serverTime => DateTime.fromMillisecondsSinceEpoch(at!);
}
